package online.niuma.xiaocubao.controller;

import online.niuma.xiaocubao.pojo.vo.TinyMceVo;
import online.niuma.xiaocubao.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author 一颗蛋50斤
 * IntelliJ IDEA
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Resource
    private QiniuUtils qiniuUtils;

    @PostMapping("/qiniu")
    public TinyMceVo uploadQiNiu(@RequestParam("file") MultipartFile file) {
        // 获取到文件名
        String originalFilename = file.getOriginalFilename();
        // 唯一文件名称
        String fileName = UUID.randomUUID() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        String imageUrl = qiniuUtils.upload(file, fileName);
        TinyMceVo tinyMceVo = new TinyMceVo();
        tinyMceVo.setLocation(imageUrl);
        return tinyMceVo;
    }
}
